<?
class ShopcartController extends BaseController{
    var $cart = array();
    var $list_product = array();

    public function __construct(){
        parent::__construct();
        $this->cart = $this->getArrayCart();
        $this->list_product = $this->selectProductList();
    }
    public function getArrayCart(){
        if($this->cart) return $this->cart;
        $this->cart = json_decode(base64_decode(getValue('cart','str','COOKIE','')),1);
        return $this->cart;
    }

    /*
     * tương tác với layout - trả về bảng product trong giỏ hàng
     */
    public function getProductCart(){
        $this->list_product = $this->selectProductList();
        if(!$this->list_product){
            return 'Không có sản phẩm nào trong giỏ hàng';
        }
        return ShopcartLayout::list_product_in_cart($this->list_product);
    }
    //query select product
    public function selectProductList(){
        if($this->list_product){
            return $this->list_product;
        }
        $list_product = array();
        //tong so tien cua tat ca product trong gio hang
        $total = 0;
        if(!$this->cart){
            return $list_product;
        }
        foreach($this->cart as $id=>$number){
            $db_query = new db_query('SELECT pro_id,pro_thumb,pro_name,pro_price
                                  FROM products
                                  WHERE pro_active = 1
                                  AND pro_id = '.$id);
            $row = mysql_fetch_assoc($db_query->result);unset($db_query);
            $tmp = array(
                'name'=>$row['pro_name'],
                'id'=>$row['pro_id'],
                'thumb'=>PATH_PICTURE_PRODUCT_SMALL . $row['pro_thumb'],
                'price'=>format_price($row['pro_price']),
                'total'=>format_price($row['pro_price'] * $number),
                'number'=>$number,
                'url'=>generate_product_detail_url($row)
            );
            $total += $row['pro_price'] * $number;
            //add to array product
            $list_product['list'][] = $tmp;

        }
        $list_product['total'] = format_price($total);
        //Gán ngược lại cho biến list product
        $this->list_product = $list_product;
        return $list_product;
    }
}
?>